package com.be.beadmin.system.codeGenerator;

import com.sun.corba.se.spi.ior.Identifiable;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.Session;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.internal.util.config.ConfigurationHelper;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;

import java.io.Serializable;
import java.util.Properties;

import static org.hibernate.id.enhanced.SequenceStyleGenerator.*;

/**
 * @Description:
 * @author: Jiaming Zheng
 * @Date: Create in 14:44 2023/2/11
 * @Modified By:
 **/
public class MyKeyGenerator implements IdentifierGenerator, Configurable {


    private String sequenceCallSyntax;
    private String swquence;
    /**
     * Configure this instance, given the value of parameters
     * specified by the user as <tt>&lt;param&gt;</tt> elements.
     * This method is called just once, following instantiation.
     *
     * @param type            The id property type descriptor
     * @param params          param values, keyed by parameter name
     * @param serviceRegistry Access to service that may be needed.
     */
    @Override
    public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
        final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
        final Dialect dialect = jdbcEnvironment.getDialect();

        final String sequencePerEntitySuffix = ConfigurationHelper.getString(CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params, DEF_SEQUENCE_SUFFIX);

        final String defaultSequenceName = ConfigurationHelper.getBoolean(CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false)
                ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix
                : DEF_SEQUENCE_NAME;

        sequenceCallSyntax = dialect.getSequenceNextValString(ConfigurationHelper.getString(SEQUENCE_PARAM, params, defaultSequenceName));

    }

    /**
     * Generate a new identifier.
     *
     * @param session The session from which the request originates
     * @param object  the com.be.beadmin.device.entity or collection (idbag) for which the id is being generated
     * @return a new identifier
     * @throws HibernateException Indicates trouble generating the identifier
     */
    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        if (object instanceof Identifiable) {
            Identifiable identifiable = (Identifiable) object;
            Serializable id = identifiable.getId();
            if (id != null) {
                return id;
            }
        }
        long seqValue = ((Number) Session.class.cast(session)
                .createSQLQuery(sequenceCallSyntax)
                .uniqueResult()).longValue();

        return "CTC" + seqValue;
    }
}
